#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：back 
@File    ：article_svc.py
@IDE     ：PyCharm 
@Author  ：七分是你
@Date    ：2024/7/13 下午2:14 
@explain : 
"""
import json
import math
import time

from app import dao, utils
from app.types import appproto


class ArticleService:
    @classmethod
    def get_article_list(cls, cid: int = 0, is_display: int = 0, key_type: str = '', keyword: str = '', page: int = 1,
                         pagesize: int = 10) -> utils.HttpResponse:
        where = {}
        if cid > 0:
            where['cid'] = cid
        if is_display > 0:
            where['is_display'] = is_display
        if key_type != '' and keyword != '':
            where[key_type] = '%' + keyword + '%'
        total, result = dao.ArticleQueryDao.list(page=page, page_size=pagesize, **where)
        if total == 0:
            res = {'items': [], 'total': 0, 'current_page': page, 'pagesize': pagesize,
                   'total_page': math.ceil(total / pagesize)}
            return utils.ResponseSuccess(res)
        records_list = []
        for category in result:
            cname = dao.ArticleQueryDao.get_category_by_id(category.cid)
            temp = {"id": category.id,
                    "cid": category.cid,
                    "title": category.title,
                    "content": category.content,
                    "attach": category.attach,
                    "thumb": category.thumb,
                    "is_display": category.is_display,
                    "link_url": category.link_url,
                    "seo_keywords": category.seo_keywords,
                    "seo_description": category.seo_description,
                    "click": category.click,
                    "addtime": utils.StringUtil.format_timestamp_to_str(category.addtime),
                    "sort_id": category.sort_id,
                    "source": category.source,
                    "source_reprint": category.source_reprint,
                    "cname": cname.name,
                    "link": category.link_url,
                    "display": True if category.is_display else False,
                    }
            records_list.append(temp)
        res = {'items': records_list, 'total': total, 'current_page': page, 'pagesize': pagesize,
               'total_page': math.ceil(total / pagesize)}
        return utils.ResponseSuccess(res)

    @classmethod
    def article_add(cls, input_data: appproto.ArticleInputData) -> utils.HttpResponse:
        indata = input_data
        # indata.addtime = int(time.time())
        if indata.attach:
            indata.attach = json.dumps(indata.attach, ensure_ascii=False)
        dao.ArticleOperateDao.save(indata.model_dump())
        return utils.ResponseSuccess("添加成功")

    @classmethod
    def article_updata(cls, input_data: appproto.ArticleInputData) -> utils.HttpResponse:
        article = dao.ArticleOperateDao.updata(input_data.model_dump())
        return utils.ResponseSuccess('更新成功')

    @classmethod
    def article_del(cls, ids: list[int]) -> utils.HttpResponse:
        for v in ids:
            dao.ArticleOperateDao.delete(v)
        return utils.ResponseSuccess("删除成功")

    @classmethod
    def category_list(cls, page: int = 1, pagesize: int = 10) -> utils.HttpResponse:
        total, result = dao.ArticleQueryDao.get_category_list(page=page, page_size=pagesize)
        if total == 0:
            res = {'items': [], 'total': 0, 'current_page': page, 'pagesize': pagesize,
                   'total_page': math.ceil(total / pagesize)}
            return utils.ResponseSuccess(res)
        records_list = []
        for category in result:
            temp = {
                "id": category.id, "name": category.name, "sort_id": category.sort_id, "seo_title": category.seo_title,
                "seo_description": category.seo_description, "seo_keywords": category.seo_keywords,
                "is_sys": category.is_sys
            }
            records_list.append(temp)
        res = {'items': records_list, 'total': total, 'current_page': page, 'pagesize': pagesize,
               'total_page': math.ceil(total / pagesize)}
        return utils.ResponseSuccess(res)

    @classmethod
    def category_updata(cls, data: appproto.ArticleCategoryInput) -> utils.HttpResponse:
        category = dao.ArticleOperateDao.category_updata(data.model_dump())
        res = {
            "id": category.id, "name": category.name, "sort_id": category.sort_id, "seo_title": category.seo_title,
            "seo_description": category.seo_description, "seo_keywords": category.seo_keywords,
            "is_sys": category.is_sys
        }
        return utils.ResponseSuccess(res)

    @classmethod
    def categoy_add(cls, data: appproto.ArticleCategoryInput) -> utils.HttpResponse:
        category = dao.ArticleOperateDao.category_save(data.model_dump())
        if category.id:
            return utils.ResponseSuccess("添加成功")
        return utils.ResponseFail('添加失败', 10000)

    @classmethod
    def categoy_del(cls, ids: list[int]) -> utils.HttpResponse:
        for v in ids:
            dao.ArticleOperateDao.category_del(v)
        return utils.ResponseSuccess("删除成功")
